<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class ShowProdController extends AdminBaseController {
    public function __construct(){
        parent::__construct();
        if(empty($_SESSION['user']['id'])){
            $this->error('未登录,非法访问',U('Admin/Index/admin'),0);
        }
    }
    //商品添加
      public function addpro(){
          $data=M('cate')->where("fid=0")->select();
          $this->assign('cate',$data);
          $this->display();
      }
      public function docate_list(){
          $data=M('cate')->where(array("fid"=>I('get.id')))->select();
          $this->ajaxReturn($data);
      }
      public function doaddpro(){
          $arr=array();
          foreach (I('get.') as $v){
          if($v==''){
            $arr['status']=0; 
            $arr['msg']='数据填写不完整';
            $this->ajaxReturn($arr);
            exit; 
          }  
         } 
          session('pro',I('get.'));
          $arr['status']=1;
          $this->ajaxReturn($arr);
      }
      public function addpronext(){
          $data=M('brand')->select();
          $this->assign('brand',$data);
          $this->display();
          
      }
      public function addphoto(){
          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize   = 3145728 ;// 设置附件上传大小
          $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->rootPath  = './Uploads/prod/'; // index设置附件上传根目录
          $upload->savePath  = ''; // 设置附件上传（子）目录
          // 上传文件
          $info   =   $upload->upload();
          $goods_pic=$info[0]['savepath'].$info[0]['savename'];
          array_shift($info);
          foreach($info as $v){
              $str.=$v['savepath'].$v['savename'].";";
          }
          $str=rtrim($str,';');
        
          $data['cate_id']=session('pro.cate_id');
          $data['goods_name']=session('pro.goods_name');
          $data['goods_pic']=$goods_pic;
          $data['goods_attr_pic']=$str;
          $data['up_down']=session('pro.up_down');
          $data['goods_desc']=session('pro.goods_desc');
          $data['goods_price']=I('post.price');
          $data['goods_num']=I('post.num');
          $data['brand_id']=I('post.brand');
          $re=M('goods')->add($data);
          if($re){
              $this->success('添加成功');
          }else{
              $this->error('添加失败');
          }
          
      }
    
      public function uploadphoto(){
          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize   = 3145728 ;// 设置附件上传大小
          $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->rootPath  = './Uploads/prod/'; // index设置附件上传根目录
          $upload->savePath  = ''; // 设置附件上传（子）目录
          // 上传文件
          $info   =   $upload->upload();
       
          foreach ($info as $k=>$v){
              if ($v['key']=='goods_pic'){
                  $goods_pic[]=$v['savepath'].$v['savename'];
              }
              if($v['key']=='goods_attr_pic'){
                  $goods_attr_pic[]=$v['savepath'].$v['savename'];
              }
          }
          //只修改大图
          if(isset($goods_pic)){
              $data['cate_id']=session('pro.cate_id');
              $data['goods_name']=session('pro.goods_name');
              $data['goods_pic']=$goods_pic[0];
              $data['up_down']=session('pro.up_down');
              $data['goods_desc']=session('pro.goods_desc');
              $data['goods_price']=I('post.price');
              $data['goods_num']=I('post.num');
              $data['brand_id']=I('post.brand');
              $re=M('goods')->filter('strip_tags')->where(array('id'=>session('proid')))->setField($data);
              if($re){
                  $this->success('修改成功',U('Goods/goods'));
                  exit;
              }
          }
          //不修改图片
        if(count($goods_attr_pic)==0){
            $data['cate_id']=session('pro.cate_id');
            $data['goods_name']=session('pro.goods_name');
            $data['up_down']=session('pro.up_down');
            $data['goods_desc']=session('pro.goods_desc');
            $data['goods_price']=I('post.price');
            $data['goods_num']=I('post.num');
            $data['brand_id']=I('post.brand');
            $re=M('goods')->filter('strip_tags')->where(array('id'=>session('proid')))->setField($data);
            if($re){
                $this->success('修改成功',U('Goods/goods'));
                exit;
            }   
        }
        
        $arr=M('goods')->where(array('id'=>session('proid')))->find();
        $attr=explode(';',$arr['goods_attr_pic']);
        //删除图片库图片
        foreach ($attr as $vo){
            chmod('/Uploads/prod',0777);
            unlink('Uploads/prod/'.$vo);
        }
        //获取要上传文件的值
          $str='';
          foreach ($goods_attr_pic as $vo){
              $str.=$vo.";";
          }
          ////只修改小图
          $goods_attr_pic=rtrim($str,';');    
          $data['cate_id']=session('pro.cate_id');
          $data['goods_name']=session('pro.goods_name');
          $data['goods_attr_pic']=$goods_attr_pic;
          $data['up_down']=session('pro.up_down');
          $data['goods_desc']=session('pro.goods_desc');
          $data['goods_price']=I('post.price');
          $data['goods_num']=I('post.num');
          $data['brand_id']=I('post.brand');
         $re=M('goods')->filter('strip_tags')->where(array('id'=>session('proid')))->setField($data);
          if($re){
              $this->success('修改成功',U('Goods/goods'));
          }else{
              $this->error('修改失败');
          }
      }
     public function protype(){
         $data=M('brand')->select();
         $this->assign('brand',$data);
         $this->display();
     }
  
     public function editgoods(){
         //商品id存储session
        session('proid',I('get.id')); 
        $re=M('goods_brand')->where(array('id'=>I('get.id')))->find();
        $this->assign('pro',$re);
        $this->display();
    }
    public function doeditpro(){
        $arr=array();
        foreach (I('get.') as $v){
            if($v==''){
            $arr['status']=0;
            $arr['msg']='数据填写不完整';
            $this->ajaxReturn($arr);
            exit;
        }
      }
        session('pro',I('get.'));
        $arr['status']=1;
        $this->ajaxReturn($arr);
    }
    public function editpronext(){
        $img=M('goods')->where(array('id'=>session('proid')))->find();
        $imgs=explode(';',$img['goods_attr_pic']);
        $data=M('brand')->select();
        $this->assign('img',$img['goods_pic']);
        $this->assign('imgs',$imgs);
        $this->assign('brand',$data);
        $this->assign('good',$img);
        $this->display();
    }
    public function edittype(){
        $data=M('goods_brand')->where(array(''=>I('get.id')))->find();
        $brand=M('brand')->select();
        $this->assign('brand',$brand);
        $this->assign('price',$data);
        $this->display();
    }
   

    
    
    
    
    
    
    
    
    
}